/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package src.view.loan;

import java.io.Serializable;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.model.SelectItem;
import org.primefaces.context.RequestContext;
import org.primefaces.event.CloseEvent;
import src.domain.loan.Loanfee;
import src.services.gl.GlaccntModel;
import src.services.loan.LoanfeeModel;
import src.util.Messager;
import src.util.Utils;

/**
 *
 * @author jay
 */

@ManagedBean
@SessionScoped
public class LoanfeeBean implements Serializable {

    private List<Loanfee> fees;
    private List<SelectItem> availableGL;
    private Loanfee selectedData, unsaveData;
    private GlaccntModel glHelper;
    private LoanfeeModel feeHelper;
    private boolean editMode=true;

    public LoanfeeBean() {
        this.feeHelper = new LoanfeeModel();
        this.glHelper = new GlaccntModel();
        this.fees = feeHelper.findAll(null);
        this.availableGL = glHelper.listAll();
        this.selectedData = null;
        this.unsaveData = null;
    }

    public List<Loanfee> getFees() {
        return fees;
    }

    public Loanfee getSelectedData() {
        return selectedData;
    }

    public void setSelectedData(Loanfee selectedData) {
        this.selectedData = selectedData;
    }

    public Loanfee getUnsaveData() {
        return unsaveData;
    }

    public void setUnsaveData(Loanfee unsaveData) {
        this.unsaveData = unsaveData;
    }
        
    public List<SelectItem> getAvailableGL() {
        return availableGL;
    }

    public String selectItemValueOf(List<SelectItem> avblList, Integer id) {
        return (String) Utils.selectItemValueOf(avblList, id);
    }
    
    public void handleClose(CloseEvent event) {
        this.selectedData = null;
        this.unsaveData = null;
    }
    
    public void initForm() {
        this.availableGL = glHelper.listAll();
    }
    
    public void openData() { 
        initForm();
        this.editMode = true;
        this.unsaveData = Loanfee.clone(selectedData);
    }
    
    public void newData() {  
        initForm();
        this.selectedData = null;
        this.unsaveData = new Loanfee();
        this.editMode = false;
    }
    
    public void save() {
        RequestContext context = RequestContext.getCurrentInstance();  
     
        boolean success = feeHelper.saveData(this.unsaveData);
        if (success==true) {
            if (this.editMode==false) this.fees.add(unsaveData);
            else {
                int i = fees.indexOf(selectedData);
                if (i <= fees.size()) fees.set(i, Loanfee.clone(unsaveData));
            }
            editMode = true;
            
        } else {
            if (this.editMode==false) unsaveData.setFeeId(null);
        }
                                        
        Messager.showSaveUpdateStatus(success);
        context.addCallbackParam("success", success);  
    }
    
    public void delete() {
        
        RequestContext context = RequestContext.getCurrentInstance();  
        boolean success = feeHelper.deleteData(this.selectedData);
        
        if (success) {
            this.fees.remove(this.selectedData);
            this.selectedData = null;
        }
               
        Messager.showDeleteStatus(success);
        context.addCallbackParam("success", success);  
    }
    
}
